package cn.mxb.logic.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.mxb.floor.dao.impl.BaseDaoImpl;
import cn.mxb.floor.pagination.Result;
import cn.mxb.logic.entity.SysFamily;

@Repository("sysFamilyDAO")
public class SysFamilyDAO  extends BaseDaoImpl<SysFamily>   {

	/**
	 * 分页
	 * @return_type: Result<SysFamily>
	 */
	@SuppressWarnings("unchecked")
	public Result<SysFamily> find(SysFamily sysFamily, int page, int size){
		String queryString = "from SysFamily where 1=1";
		if( null != sysFamily.getFamilyName()  && !"".equals(sysFamily.getFamilyName())){
			queryString = queryString +" and familyName = '"+sysFamily.getFamilyName()+"' ";
		}
		if( null != sysFamily.getFamilyId()  && !"".equals(sysFamily.getFamilyId())){
			queryString = queryString +" and familyId = '"+sysFamily.getFamilyId()+"' ";
		}
		int start=(page-1)*size;
		int limit =size;
		String msg = "";
		return (Result<SysFamily>)super.find(queryString, null, null, start, limit,msg);
	}
	
	/**
	 * 根据字段property的值value查询
	 */
	@SuppressWarnings("unchecked")
	public List<SysFamily> findListByProperty(String property,String value){
		List<SysFamily> list = null;
		String queryString = "from SysFamily where "+property+" like '%"+value+"%'";
		
		try{
		   list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	
	/**
	 * 
	 * 根据字段值查询(实体)
	 */
	@SuppressWarnings("unchecked")
	public SysFamily findByProperty(String property, String value) {
		List<SysFamily> list = null;
		SysFamily sysFamily = null;
		String queryString = "from SysFamily where "+property+" like '%"+value+"%' ";
		try{
		   list = this.getHibernateTemplate().find(queryString);
		   if(list.size()>0 && list !=null){
			   sysFamily = list.get(0);
		   }else{
			   System.out.println("SysFamily kong");
		   }
		}catch(Exception e){
			System.out.println(e);
		}
		return sysFamily;
	}
	/**
	 * 两个条件下的查询
	 * @param property
	 * @param value
	 * @param property2
	 * @param value2
	 * @return
	 */
	public List<SysFamily> findListByPropertyTwo(String property, String value, String property2, String value2) {
		List<SysFamily> list = null;
		String queryString = "from SysFamily where "+property+" like '%"+value+"%' and "+property2+" like '%"+value2+"%'  ";
		try{
		   list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	/**
	 * 修改table，根据property的value修改field为fieldvalue
	 */
	public void updatetablebyproperty(String table,String field,String property,String value,int fieldvalue) {
         System.out.println("update"+table);
	     String hql = " update "+table+" set "+field+" = ? where "+property+" = ? ";
	     Object values[] = {fieldvalue,value};
	     try{
	    	 update(hql, values);
	     }catch(Exception e){
	    	 System.out.println(e);
	     }
		 System.out.println("update");
	}
}
